package vg.modules.zoom;

import vg.modules.zoom.components.ZoomBox;
import vg.modules.zoom.components.ZoomIn;
import vg.modules.zoom.components.ZoomOut;
import vg.services.graph_view_manager.interfaces.graph_comparison.AGraphComparisonView;
import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;
import vg.services.graph_view_manager.interfaces.tab.ATab;
import vg.services.main_manager.VGMainServiceHelper;
import vg.services.plugin_manager.event_and_request.event.AUIEvent;
import vg.services.plugin_manager.event_and_request.event.UIEventChangeTab;
import vg.services.plugin_manager.interfaces.IPlugin;
import vg.services.user_interface_manager.interfaces.AUserInterfaceElement;
import vg.services.user_interface_manager.interfaces.UserInterfaceObserverAdapter;

import javax.swing.*;

/**
 * This plugin installs scaling panel.
 *
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class ZoomPlugin extends AUserInterfaceElement implements IPlugin {
    // Mutex
    private final Object generalMutex = new Object();

    // Main components
    private ZoomIn zoomIn;
    private ZoomOut zoomOut;
    private ZoomBox zoomBox;
    private JToolBar toolBar;

    /**
     * Constructor.
     */
    public ZoomPlugin() {
        super("Zoom panel", null);
    }

    /**
	 * Installer.
	 */
	public void install() throws Exception {
        // initialize components
        zoomIn = new ZoomIn();
        zoomOut = new ZoomOut();
        zoomBox = new ZoomBox();

        //toolBar = new JPanel(new GridBagLayout());
        toolBar = new JToolBar();
        toolBar.setFloatable(false);

        toolBar.add(zoomBox.getView());
        toolBar.add(zoomIn.getView());
        toolBar.add(zoomOut.getView());

        VGMainServiceHelper.userInterfaceManager.addInstrument(this);

        VGMainServiceHelper.userInterfaceManager.addObserver(new UserInterfaceObserverAdapter() {
            @Override
            public void dispatchEvent(AUIEvent event) {
                switch (event.getType()) {
                    case DEF_CHANGE_UI_STYLE: {
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                synchronized (generalMutex) {
                                    SwingUtilities.updateComponentTreeUI(toolBar);
                                }
                            }
                        });
                        break;
                    }

                    case DEF_CHANGE_TAB: {
                        UIEventChangeTab bufEvent = (UIEventChangeTab)event;
                        ATab tab = bufEvent.getTab();
                        if (tab != null) {
                            switch (tab.getTabType()) {
                                case ATab.DEF_GRAPH_VIEW_TAB_TYPE: {
                                    AGraphView igv = (AGraphView)tab;
                                    zoomIn.changeView(igv);
                                    zoomOut.changeView(igv);
                                    zoomBox.changeView(igv);
                                    break;
                                }

                                case ATab.DEF_GRAPH_COMPARISON_TAB_TYPE: {
                                    AGraphView igv = ((AGraphComparisonView)tab).getMainGraphView();
                                    zoomIn.changeView(igv);
                                    zoomOut.changeView(igv);
                                    zoomBox.changeView(igv);
                                    break;
                                }

                                default:
                                    break;
                            }
                        }
                        break;
                    }

                    default:
                        break;
                }
            }
        });
    }

    @Override
    public JComponent getView() {
        return toolBar;
    }
}
